
//
//  VistorView.swift
//  swiftImitationWeibo
//
//  Created by 杨果果 on 16/7/9.
//  Copyright © 2016年 yang. All rights reserved.
//

import UIKit
import SnapKit
class VistorView: UIView {
//点击事件的闭包
    var clourse: (()->())?
    //添加旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //添加蒙版
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //主页图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //添加label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.grayColor()
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        return label
    }()
    //添加注册button
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.addTarget(self, action: "registerButtonAction", forControlEvents: .TouchUpInside)
        //背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
    //添加登录button
    private lazy var loginButton: UIButton = {
       let button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.addTarget(self, action: "loginButtonAction", forControlEvents: .TouchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
    
    //重写frame
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加约束
    private func setupUI(){
        backgroundColor = UIColor(white: 235/255.0, alpha: 1)
        //先添加在设置约束
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(messageLabel)
        
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }

    }
    //开始转动
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
        
    }
    @objc private func registerButtonAction() {
        clourse?()
    }
    @objc private func loginButtonAction() {
        clourse?()
    }
    

    func updataVisitorViewInfo(title: String?,imageName: String?){
        if let t = title, imgName = imageName{
        messageLabel.text = t
        
        iconImageView.image = UIImage(named: imgName)
        cycleImageView.hidden = true
    }else{
        cycleImageView.hidden = false
            startAnimation()
    
    }
    
    
    
    }
    
    
    
}
